<?php

if (!function_exists('lang')) {
    /**
     * 语言包内容
     * @param integer $code 语言包id
     * @return string
     */
    function lang($code)
    {
        return __("errno.{$code}");
    }
}

if (!function_exists('yqresponse')) {
    /**
     * 接口返回错误码和详细内容
     *
     * @param integer $code 错误码
     * @param string $msg 内容
     * @param array $params 详情
     * @return Illuminate\Http\Response
     */
    function yqresponse(int $code, string $msg, array $params = [])
    {
        $content = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $params,
            'time' => time(),
        ];

        return response(json_encode($content, JSON_UNESCAPED_UNICODE), 200)
            ->header('Content-Type', 'application/json;charset=UTF-8');
    }
}

if (!function_exists('yqok')) {
    /**
     * 返回成功
     *
     * @param array $params
     * @return Illuminate\Http\Response
     */
    function yqok($params = [])
    {
        return yqresponse(0, 'success', $params);
    }
}

if (!function_exists('yqerr')) {
    /**
     * 返回失败
     *
     * @param int $code 错误码
     * @return Illuminate\Http\Response
     */
    function yqerr($code)
    {
        $msg = lang($code);
        return yqresponse($code, $msg);
    }
}

if (!function_exists('ctlFalse')) {
    /**
     * 控制层处理条件不通过返回失败
     *
     * @return Illuminate\Http\Response
     */
    function ctlFalse($code = 102)
    {
        $params = [];
        if (config('app.debug')) {
            $backtrace = debug_backtrace();
            foreach ($backtrace as $key => $val) {
                if ($key >= 10) {
                    break;
                }
                $params[] = [
                    'file'     => $val['file'],
                    'line'     => $val['line'],
                    'function' => $val['function'],
                ];
            }
        }

        $msg = lang($code);
        return yqresponse($code, $msg, $params);
    }
}

if (!function_exists('yqlog')) {
    /**
     * 打印日志
     * @return void
     */
    function yqlog()
    {
        $args     = func_num_args();
        $arg_list = func_get_args();
        if ($args <= 0) {
            return;
        }

        $array = debug_backtrace();
        $row   = $array[0];
        $msg   = "[RUNTIME] " . $row['file'] . ':' . $row['line'] . "\t\t";
        foreach ($arg_list as $value) {
            if (is_array($value)) {
                $msg = $msg . json_encode($value, JSON_UNESCAPED_UNICODE) . "\t";
            } else {
                $msg = $msg . $value . "\t";
            }
        }
        \Illuminate\Support\Facades\Log::info($msg);
    }
}

if (!function_exists('true')) {
    function true()
    {
        return [true, 0];
    }
}

if (!function_exists('false')) {
    function false($errno)
    {
        return [false, $errno];
    }
}

/**
 * 输出xml字符
 * @param $params
 * @return bool|string
 */
if (!function_exists('toXml')) {
    function toXml($params)
    {
        if (empty($params)) {
            return false;
        }

        $xml = '<xml>';
        foreach ($params as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<{$key}>{$val}</{$key}>";
            } else {
                $xml .= "<{$key}><![CDATA[{$val}]]></{$key}>";
            }
        }
        $xml .= '</xml>';

        return $xml;
    }
}

/**
 * 将xml转为array
 * @param $xml
 * @return array|bool|mixed
 */
if (!function_exists('fromXml')) {
    function fromXml($xml)
    {
        if (!$xml) {
            return false;
        }
        //将XML转为array

        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }
}

if (!function_exists('week_schedule')) {
    /**
     * 判断time在是否落在时间控件 week_schedule 下
     * @param int    $time         时间戳
     * @param string $schedule_str 周一至周日，每半小时算1个，总数为336个组成
     * @return boolean
     */
    function week_schedule($time, $schedule_str)
    {
        $week = date("w", $time);
        if ($week === 0) {
            $week = 7;
        }
        $daynum = ($week - 1) * 48 + date('H', $time) * 2 + (date('i', $time) <= 30 ? 1 : 2) - 1;
        $status = substr($schedule_str, $daynum, 1);

        return $status == 1;
    }
}

// 把字符串变为int，把负数归0
if (!function_exists('toUnsignedInt')) {
    function toUnsignedInt($num)
    {
        $num = intval($num);
        if ($num < 0) {
            $num = 0;
        }

        return $num;
    }
}
